# -*- encoding: utf-8 -*-
"""
File Name: launch
Description:
Author : 'lirui'
date: 2024/2/1
Contact: marjey_lee@163.com
Change Activity:
2024/2/1:
"""
import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

import ll
from projects.monitoring.branch_dedicated_line_monitor.services.branch_dedicated_line_monitor_pipeline import BranchDedicatedLineMonitorPipeline

_APP = FastAPI(
    title="Dedicated line monitor ",
    description="Dedicated line monitor ",
    version="1.0.1",
)
_MONITOR = BranchDedicatedLineMonitorPipeline()
_MONITOR.start()
_APP.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=False,
    allow_methods=["*"],
    allow_headers=["*"],
)


@_APP.get("/branch_dedicated_line/{ip}")
def read_item(ip: str):
    if ip not in _MONITOR.results:
        ll.get_logger().warning(f'{ip} not in results list.')
        return 0
    return int(_MONITOR.results[ip])


@_APP.get("/get_all_branch_dedicated_lines")
def read_item():
    return _MONITOR.results


@_APP.get("/health_check")
def main():
    return _MONITOR.is_running_normal


def _main():
    uvicorn.run(_APP, host="0.0.0.0", port=8080, log_level='warning')


if __name__ == '__main__':
    _main()
